package com.cqc.listviewchoicedemo;

import android.os.Bundle;
import android.support.annotation.IntegerRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.TextView;

import com.cqc.listviewchoicedemo.bean.Person;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ${cqc} on 2017/3/13.
 */

public class CustomItemSingleChoiceActivity extends AppCompatActivity {

    private static final String TAG = "复杂item单选";
    private ListView listView;
    private Button btn;
    private List<Person> perList = new ArrayList<>();
    private int selectPosition = Integer.MAX_VALUE;
    private MyAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_single_choice);
        initData();
        findViews();
        initViews();
    }

    private void initData() {
        for (int i = 0; i < 100; i++) {
            Person person = new Person();
            person.setName("张" + i);
            person.setAge(i);
            person.setSelected(false);
            perList.add(person);
        }
    }

    private void initViews() {
        //设置选择模式有2种方法：xml和java设置
//        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                //单选需要把其他的改成“不选”
                for (int i = 0; i < perList.size(); i++) {
                    if (i == position) {
//                        perList.get(i).setSelected(true); //必须选中一个
                        perList.get(i).setSelected(!perList.get(i).isSelected());//可以不选
                        Log.d(TAG, "position=" + position + "点击即选中：" + perList.get(i).isSelected());
                    } else {
                        perList.get(i).setSelected(false);
                    }
                }
                selectPosition = position;
                adapter.notifyDataSetChanged();
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //当必须选中一个时
//                Log.d(TAG, "selectPosition=" + selectPosition);
                //当可以不选时
                if (perList.get(selectPosition).isSelected()) {
                    Log.d(TAG, "您选中的是=" + selectPosition);
                } else {
                    Log.d(TAG, "您没有选择");
                }

                int checkedItemPosition = listView.getCheckedItemPosition();
                Log.d(TAG, "checkedItemPosition="+checkedItemPosition);
            }
        });
    }

    private void findViews() {
        listView = (ListView) findViewById(R.id.listView);
        btn = (Button) findViewById(R.id.btn);
    }


    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return perList.size();
        }

        @Override
        public Object getItem(int position) {
            return perList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            MyViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_single__custom_choice, viewGroup, false);
                holder = new MyViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (MyViewHolder) convertView.getTag();
            }
            Person person = perList.get(position);
            holder.tv_choice.setText("姓名：" + person.getName());
            holder.tv_choice2.setText("年龄：" + person.getAge());
            holder.checkTV.setChecked(person.isSelected());
            return convertView;
        }
    }

    public class MyViewHolder {
        TextView tv_choice;
        TextView tv_choice2;
        CheckedTextView checkTV;

        public MyViewHolder(View itemView) {
            checkTV = (CheckedTextView) itemView.findViewById(R.id.checkTV);
            tv_choice = (TextView) itemView.findViewById(R.id.tv_choice);
            tv_choice2 = (TextView) itemView.findViewById(R.id.tv_choice2);
        }
    }
}
